﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using Lists;
using GeneralLibs;
using System.Linq;
using System.Windows.Forms;

namespace Vouchers
{
    public class ThongSoMacDinhHoaDonBanle
    {

        #region TaiKhoanNoTienThuVe property
        private TaiKhoan _TaiKhoanNoTienThuVe;
        public TaiKhoan TaiKhoanNoTienThuVe
        {
            get
            {
                return _TaiKhoanNoTienThuVe;
            }
            set
            {
                _TaiKhoanNoTienThuVe = value;
            }
        }
        #endregion

        #region TaiKhoanCoDoanhThu property
        private TaiKhoan _TaiKhoanCoDoanhThu;
        public TaiKhoan TaiKhoanCoDoanhThu
        {
            get
            {
                return _TaiKhoanCoDoanhThu;
            }
            set
            {
                _TaiKhoanCoDoanhThu = value;
            }
        }
        #endregion

        #region TaiKhoanCoVat property
        private TaiKhoan _TaiKhoanCoVat;
        public TaiKhoan TaiKhoanCoVat
        {
            get
            {
                return _TaiKhoanCoVat;
            }
            set
            {
                _TaiKhoanCoVat = value;
            }
        }
        #endregion

        #region TaiKhoanNoGiaVon property
        private TaiKhoan _TaiKhoanNoGiaVon;
        public TaiKhoan TaiKhoanNoGiaVon
        {
            get
            {
                return _TaiKhoanNoGiaVon;
            }
            set
            {
                _TaiKhoanNoGiaVon = value;
            }
        }
        #endregion
        
        #region TaiKhoanCoXuatKho property
        private TaiKhoan _TaiKhoanCoXuatKho;
        public TaiKhoan TaiKhoanCoXuatKho
        {
            get
            {
                return _TaiKhoanCoXuatKho;
            }
            set
            {
                _TaiKhoanCoXuatKho = value;
            }
        }
        #endregion

        #region ThueSuatMacDinh property
        private string _ThueSuatMacDinh;
        public string ThueSuatMacDinh
        {
            get
            {
                return _ThueSuatMacDinh;
            }
            set
            {
                _ThueSuatMacDinh = value;
            }
        }
        #endregion
    }
    public partial class ChungTu
    {
        
        private void GhiSoButToanHoaDonBanLe(SqlCommand cmd)
        {
            //But toan thanh toan
            ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                    this.TaiKhoan1ID, this.TaiKhoan4ID, this.TienTeID, this.TyGiaThucTe, this.Value6,
                                    "Giá trị hóa đơn bán lẻ " + this.So);
            //but toan vat
            ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                    this.TaiKhoan1ID, this.TaiKhoan2ID, this.TienTeID, this.TyGiaThucTe, this.Value5,
                                    "VAT hóa đơn" + this.So, this.TenNguoiThucHien, DanhMucDoiTuongNghiepVu.Instance.GetByID(this.DoiTuongNghiepVu1ID).MaSoThue, this.Text1, this.So, this.Ngay,
                                    Guid.Empty, 0, this.ThueSuat1ID);
           
            foreach (ChungTu phieuXuatKho in _DanhSachChungTuPhuThuoc)
            {
                //But toan xuat kho
                ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                        phieuXuatKho.TaiKhoan1ID, phieuXuatKho.TaiKhoan2ID, this.TienTeID, this.TyGiaThucTe, phieuXuatKho.SoTienNT,
                                        this.DienGiai);
            }     
        }

        public bool PrintChungTuHoaDonBanLe(bool quaCrystalReport)
        {
            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];
                GeneralLibs.LocalSettings.HeThong.TenCuaHang = firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"].ToString();
                GeneralLibs.LocalSettings.HeThong.DiaChi = firstRowOfThongTinDoanhNghiep["DiaChi"].ToString();
                GeneralLibs.LocalSettings.HeThong.DienThoai = firstRowOfThongTinDoanhNghiep["DienThoai"].ToString();

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                //ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = string.Format("({0})", GeneralLibs.CommonLib.DocTien((int)tongSoTien));
               
                cmd.CommandText = "Select_Detail_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Detail_HoaDonBanLe);
                foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                {
                    if (((string)row["TenHang"]).Length > 22)
                        row["TenHang"] = ((string)row["TenHang"]).Substring(0, 22) + "..";
                }

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();

                ReportTemplates.BanLe.HoaDonBanLeNinhBinh.RptHoaDonBanLeNinhBinh report = new ReportTemplates.BanLe.HoaDonBanLeNinhBinh.RptHoaDonBanLeNinhBinh();
                report.SetDataSource(ds);

                if (!quaCrystalReport)
                {
                    report.PrintToPrinter(1, false, 0, 0);
                }
                else
                {
                    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                    frm.Show();
                }

                ////Dùng khi không in được tự động thì in qua report
                //if (DanhMucStore.Instance.GetByStoreID(Config.StoreID).Ten.Trim().ToUpper().Contains("BachKhoa"))
                //{
                //    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                //    frm.Show();
                //}
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }
    }
}
